 $(document).ready(function () {
    var table = $('#ProductsTable').DataTable({
        dom: "<'row'<'col-sm-12 col-md-12'f>>" +
            "<'row'<'col-sm-12'tr>>" +
            "<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
        ajax: {
            url: "/api/core_product/list/",
            type: "GET",
            dataSrc: ""
        },
        columns: [
            {
                data: null,
                render: function (data) {
                    return '<a href="/products/details?item_num='+data['item_num1']+'" ' + 'style="text-decoration: none;color: black">'+ data['name1'] +'</a>'
                }
            },
            {
                data: null,
                render: function (data) {
                    return '<a href="/products/details?item_num='+data['item_num2']+'" ' + 'style="text-decoration: none;color: black"">'+ data['name2'] +'</a>'
                }
            },
            {
                data: null,
                render: function (data) {
                    return '<a href="/products/details?item_num='+data['item_num3']+'" ' + 'style="text-decoration: none;color: black"">'+ data['name3'] +'</a>'
                }
            },
            {
                data: null,
                render: function (data) {
                    return '<a href="/products/details?item_num='+data['item_num4']+'" ' + 'style="text-decoration: none;color: black"">'+ data['name4'] +'</a>'
                }
            },
        ],
        ordering: false,
        searching: false,
        bProcessing: true,
        language: {
            "sSearch": "",
            "sLengthMenu": "",
            "sZeroRecords": "",
            "sEmptyTable": "",
            "sInfo": "",
            "sInfoEmpty": "",
            "sInfoFiltered": "",
            "sProcessing": '<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>\n' +
                '<span class="sr-only">Loading...</span>',
            "sLoadingRecords": '',
        },
        paging: false,
        destroy: true,
    });
});

function dynamicSearch2(obj) {
    let search_value = $(obj).val();
    if (search_value) {
        $.ajax({
            type: "GET",
            url: "/api/core_product/filter/",
            data: {
                search_value: search_value
            },
            success: function (data) {

                $( "#dynamicSearch2" ).autocomplete({
                  source: data
                });
            }
        })
    }

}

$(document).keyup(function(event) {
    if (event.keyCode === 13) {
        makeSearch2();
    }
});


function makeSearch2() {
    let search_value = $('#dynamicSearch2').val();

    if (search_value){
        $.ajax({
            type: "GET",
            url: "/api/core_product/mapping/",
            data: {
                search_value: search_value
            },
            success: function (data) {
                if (data === {}){
                    alert("搜索结果不存在")
                } else {
                    if (data){
                        window.location.href='/products/details?item_num='+data;
                    } else {
                        alert("请选择精确搜索条件")
                    }
                }
            }
        })
    }
}